package com.lawfinal.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lawfinal.entity.User;
import com.lawfinal.entity.Writ;
import com.lawfinal.service.UserService;
import com.lawfinal.utils.QueryPageParam;
import com.lawfinal.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author KBr
 * @since 2024-04-08
 */
@RestController
@CrossOrigin("http://localhost:5173/")
@RequestMapping("/user")
public class UserController {

      @Autowired
      private UserService userService;

      //      登录
      @RequestMapping("/login")
      public Result userLogin(@RequestBody User user){
            List list=  userService.lambdaQuery()
                    .eq(User::getAccount,user.getAccount())
                    .eq(User::getPassword,user.getPassword()).list();
            return list.size()>0?Result.sucess(list.get(0)):Result.fail();
      }
      //       管理员adminManager账号列表
      @RequestMapping("/list")
      public List<User> list(){
            return userService.list();
      }

//      分页基础数据
      @RequestMapping( "/listPageResult")
      public Result listPageResult(@RequestBody QueryPageParam queryPageParam){
            System.out.println("--------------------------");
//            System.out.println(queryPageParam);

            HashMap param=queryPageParam.getParam();
            System.out.println(param);
            System.out.println((String) param.get("name"));
            String name = (String) param.get("name");
            String sex = (String) param.get("sex");

            Page<User> page = new Page();
            page.setCurrent(queryPageParam.getPageNum());
            page.setSize(queryPageParam.getPageSize());

            LambdaQueryWrapper<User> lambdaQueryWrapper= new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(name) && !"null".equals(name)){
                  lambdaQueryWrapper.like(User :: getName,name);
            }
            if (StringUtils.isNotBlank(sex) && !"null".equals(sex)){
                  lambdaQueryWrapper.like(User :: getSex,sex);
            }

            IPage result=userService.page(page,lambdaQueryWrapper);

            System.out.println(result.getTotal());

            return Result.sucess(result.getRecords(),result.getTotal());
      }

//      条件查询
//      1.列表账号查询
      @RequestMapping("findByAccount")
      public Result findByNo( String account){
            List list=userService.lambdaQuery().eq(User::getAccount,account).list();
            return list.size()>0?Result.sucess(list):Result.fail();
      }

      //      注册
      @RequestMapping("/enroll")
      public Result save(@RequestBody User user){
            return userService.save(user)?Result.sucess():Result.fail();
      }

      @RequestMapping("/update")
      public Result update(@RequestBody User user){
            return  userService.updateById(user)?Result.sucess():Result.fail();
      }
}
